// pages/agreementCustomerList/agreementCustomerList.js
import Dialog from '../../miniprogram_npm/@vant/weapp/dialog/dialog';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    height: 0,
    customerList: [],
    customerName: '',
    pagination: {
      page_start: 0, //搜索开始页码
      current_page: 0, //当前页码
      page_count: 0, //总页数
      page_size: 10, //每页数量
      total_count: 0 //总数量
    },
    isNoMore: false
  },
  onChange(e) {
    this.setData({
      customerName: e.detail.value
    })
  },
  editCustomer(e) {
    let info = JSON.stringify(e.currentTarget.dataset.info)
    wx.navigateTo({
      url: '/pages/agreementCustomerEdit/agreementCustomerEdit?info=' + info,
    })
  },
  toPackage(e) {
    let info = JSON.stringify(e.currentTarget.dataset.info)
    wx.navigateTo({
      url: '/pages/agreementCustomerPackage/agreementCustomerPackage?info=' + info,
    })
  },
  searchData() {
    this.setData({
      pagination: {
        page_start: 0, //搜索开始页码
        current_page: 0, //当前页码
        page_count: 0, //总页数
        page_size: 10, //每页数量
        total_count: 0 //总数量
      }
    })
    this.getData();
  },
  getData() {
    wx.showLoading()
    let pagination = this.data.pagination;
    pagination['page_start'] = pagination.current_page + 1;
    wx.$api.listProtocolmember({
      company_name: this.data.customerName,
      pagination: pagination
    }).then(res => {
      wx.hideLoading();
      if (res.data.code == 200) {
        let data = {
          pagination: res.data.data.pagination
        }
        if (this.data.pagination.current_page == 0) {
          data['customerList'] = res.data.data.list;
        } else {
          if (res.data.data.list.length) {
            data['customerList'] = this.data.customerList.concat(res.data.data.list);
          } else {
            data['isNoMore'] = true;
          }
          data['pagination'] = res.data.data.pagination;
        }
        this.setData(data)
      }
      if (res.data.msg == '暂无数据') {
        this.setData({
          customerList: []
        })
      }
    })
  },
  deleteCustomer(e) {
    Dialog.confirm({
      title: '确认删除',
      message: '确定删除该客户信息？',
      asyncClose: true,
      className: 'btnWidth'
    }).then(() => {
      wx.$api.deletePorotocolMember({
        id: e.currentTarget.dataset.id
      }).then(res => {
        Dialog.close();
        if (parseInt(res.data.code) != 200) {
          return wx.$Toast(res.data.msg);
        }
        this.getData();
      })
    }).catch(() => {
      Dialog.close();
    });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
    wx.getSystemInfo({
      success: function (res) {
        let query = wx.createSelectorQuery(),
          height = res.windowHeight;
        query.select('.search').boundingClientRect(function (rect) {
          that.setData({
            height: height - rect.height - 60 + 'px'
          })
          console.log(that.data.height, 'mmmmmmmm')
        }).exec();
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.searchData();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})